package com.example.passwordmanager;

import android.app.KeyguardManager;
import android.content.Intent;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.Ohuang.ilivedata.MyLiveData;
import com.example.passwordmanager.Util.FingerprintUtil;
import com.example.passwordmanager.Util.QMUIDialogUtil;
import com.example.passwordmanager.Util.Sys;
import com.example.passwordmanager.databinding.FragmentLoginBinding;
import com.example.passwordmanager.dialog.FingerprintDialogFragment;
import com.example.util.base_util.SPUtil;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;


public class LoginFragment extends Fragment {
    private FragmentLoginBinding fragmentLoginBinding;
    private View root;
    private FingerprintDialogFragment fingerprintDialogFragment;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root = inflater.inflate(R.layout.fragment_login, container, false);
        fragmentLoginBinding = FragmentLoginBinding.bind(root);
        initView();
        return root;
    }

    private void initView() {
        fragmentLoginBinding.imageClose.setOnClickListener(v -> {
            getActivity().finish();
        });

        fragmentLoginBinding.imageNext.setOnClickListener(v -> {
            fragmentLoginBinding.input.setErrorEnabled(false);
            String psd = fragmentLoginBinding.etPassword.getText().toString();
            if (TextUtils.isEmpty(psd)) {
                fragmentLoginBinding.input.setError("密码不能为空");
                return;
            }
            String s = (String) SPUtil.get(getContext(), "password", "");
            if (!TextUtils.equals(psd, s)) {
                fragmentLoginBinding.input.setError("密码错误");
                return;
            }

            startActivity(new Intent(getContext(), ListActivity.class));
            getActivity().finish();
        });

        fragmentLoginBinding.tvRecharge.setOnClickListener(v -> {
            showDialog();
        });

        fragmentLoginBinding.tvRechange.setOnClickListener(v -> {
            MyLiveData.getInstance().with("startFg", Fragment.class).setValue(new ChangePsdFragment());
        });

        fingerprintDialogFragment = new FingerprintDialogFragment(new FingerprintDialogFragment.CallBack() {
            @Override
            public void onSucceed() {
                startActivity(new Intent(getContext(), ListActivity.class));
                getActivity().finish();
                Toast.makeText(getContext(), "指纹验证成功", Toast.LENGTH_LONG).show();
            }
        });
        fragmentLoginBinding.imageIcon.setOnClickListener(v -> {
            if (Sys.fingerprintType==0&&FingerprintUtil.supportFingerprint(getContext())) {
                if (!fingerprintDialogFragment.isAdded()) {
                    fingerprintDialogFragment.show(getChildFragmentManager());
                }
            }
        });
    }

    private void showDialog() {
        QMUIDialogUtil.showMessagePositiveDialog(getActivity(), "忘记密码", "本应用将在24小时内进行锁定状态,锁定期结束才可以重新设定密码，是否开始锁定?"
                , new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        long l = System.currentTimeMillis();
                        SPUtil.put(getContext(), "time", l);
                        SPUtil.put(getContext(), "isLock", true);
                        dialog.dismiss();
                        getActivity().finish();
                    }
                });
    }


}